package com.ybxx.fees.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.StatementCallback;

import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.dao.impl.BaseDaoImpl;
import com.ybxx.core.util.UUIDGenerator;
import com.ybxx.core.web.paging.PagingBean;
import com.ybxx.fees.dao.AnmeterRecordsDao;
import com.ybxx.fees.model.AnmeterRecordItem;
import com.ybxx.fees.model.AnmeterRecords;

public class AnmeterRecordsDaoImpl extends BaseDaoImpl<AnmeterRecords>
		implements AnmeterRecordsDao {

	public AnmeterRecordsDaoImpl() {
		super(AnmeterRecords.class);
		// TODO Auto-generated constructor stub
	}

	public AnmeterRecords generator(final String sql) {
		return super.jdbcTemplate.execute(new StatementCallback<AnmeterRecords>(){
			public AnmeterRecords doInStatement(Statement stat)
					throws SQLException, DataAccessException {
				ResultSet rs=stat.executeQuery(sql);
				AnmeterRecords anmeterRecords=null;
				if(!rs.next()){
					return null;
				}
				anmeterRecords=new AnmeterRecords();
				anmeterRecords.setAmfcode(UUIDGenerator.getUUID());
				anmeterRecords.setThereadings(rs.getString("thereadings"));
				anmeterRecords.setCurrentreading(rs.getString("currentreading"));
				anmeterRecords.setWalkquantity(rs.getString("walkquantity"));
				anmeterRecords.setAmount(rs.getString("amount"));
				anmeterRecords.setCharge(rs.getString("actual"));
				float count=0.0f;
				do{
					AnmeterRecordItem item=new AnmeterRecordItem();
					item.setRicode(UUIDGenerator.getUUID());
					item.setAnmeterRecords(anmeterRecords);
					item.setTarget(rs.getString("target"));
					item.setPrice(rs.getString("price"));
					item.setWalk(rs.getString("meter"));
					item.setYmoney(rs.getString("fees"));
					count=count+Float.parseFloat((item.getYmoney()==null || "".equals(item.getYmoney()))?"0":item.getYmoney());
					anmeterRecords.getItems().add(item);
				}while(rs.next());
				anmeterRecords.setReceivable(count+"");
				return anmeterRecords;
			}
			
		});
	}

}
